package server

//type kline struct {
//	high float64
//	low float64
//	close float64
//}
//
//type StrategyWildFluctuation struct {
//	Strategy
//	OpenWave	float64	//开仓波动率
//	CloseWave	float64 //平仓波动率
//	Amount	float64	//首次购买数量
//	Interval	int64	//自定义k线间隔(秒)
//	HoldOrder models.Order	//持有订单
//	IntentOrder models.Order //委托订单
//
//	close                  chan bool
//	lck      *sync.Mutex
//	PriceMap map[int64] *kline
//	newTime  int64
//	MyKline  *kline
//	StrategyInfo *models.StrategyInfo
//
//	PriceChan chan *goex.Trade
//}
//
//func NewWildFluctuation(strategyConfig models.Strategy) (*StrategyWildFluctuation,int) {
//	baseStrategy := Strategy{
//		StrategyName: strategyConfig.StrategyName,
//		AccountName: strategyConfig.AccountName,
//		Exchange: strategyConfig.Exchange,
//		ContractType: strategyConfig.ContractType,
//		Symbol: strategyConfig.Symbol,
//	}
//	swf := StrategyWildFluctuation{
//		Strategy:baseStrategy,
//		OpenWave: strategyConfig.OpenWave,
//		CloseWave: strategyConfig.CloseWave,
//		Amount: strategyConfig.Amount,
//		Interval: strategyConfig.Interval,
//		lck : new(sync.Mutex),
//		PriceMap : make(map[int64] *kline),
//		MyKline: &kline{
//			high: -math.MaxFloat64,
//			low: math.MaxFloat64,
//		},
//		PriceChan : make(chan *goex.Trade, 10),
//	}
//	strategyKey := fmt.Sprintf("%s_%s",strategyConfig.StrategyName,strategyConfig.AccountName)
//	mycache.StrategyCache.Set(strategyKey,&swf,cache.NoExpiration)
//	code := models.AddStrategy(&strategyConfig)
//	return &swf,code
//}
//
//func StartStrategyWildFluctuation(strategyName,accountName string) int{
//	var sub ws.SubscribePriceStruct
//	strategyKey := fmt.Sprintf("%s_%s",strategyName,accountName)
//	var this *StrategyWildFluctuation
//	if x,ok := mycache.StrategyCache.Get(strategyKey) ;ok{
//		this = x.(*StrategyWildFluctuation)
//		if !this.Status {
//			priceKey := fmt.Sprintf("%s_%s_%s_price", this.Exchange, this.ContractType, this.Symbol)
//			if x,ok := mycache.PriceCache.Get(priceKey); ok {
//				sub = x.(ws.SubscribePriceStruct)
//				//添加价格订阅
//				sub.AddStrategy(this)
//				strategy_info_list,_ :=models.FindByAccountNameAndStrategyStrategyInfo(strategyName,accountName)
//				this.StrategyInfo = &strategy_info_list[0]
//				this.Status = true
//				// 更新缓存
//				mycache.StrategyCache.Set(strategyKey,this,cache.NoExpiration)
//				// 更新数据库
//				models.UpdateStrategy(strategyName,accountName,this.Status)
//				return util.START_SUSSESS_STRATEGY
//			}else {
//				log.Println("没有订阅该数据")
//				return util.NOT_SUB_PRICE
//			}
//		}else {
//			return util.EIST_START_STRATEGY_ERROR
//		}
//	}
//	return util.START_STRATEGY_ERROR_NOT_KEY
//}
//
////处理价格
//func (this *StrategyWildFluctuation)HandlePrice(trade *goex.Trade)  {
//	//1.初始化临时变量
//	temp_max := -math.MaxFloat64
//	temp_min := math.MaxFloat64
//	//2.新trade添加到priceMap
//	this.lck.Lock()
//	this.newTime = trade.Date /1000
//	if key, fund := this.PriceMap[this.newTime]; fund {
//		var k kline
//		if trade.Price < key.low {
//			k.low = trade.Price
//		}else {
//			k.low = key.low
//		}
//		if trade.Price > key.high {
//			k.high = trade.Price
//		}else {
//			k.high = key.high
//		}
//		k.close = trade.Price
//		this.PriceMap[this.newTime] = &k
//	}else {
//		this.PriceMap[this.newTime] = &kline{
//			high:  trade.Price,
//			low:  trade.Price,
//			close:  trade.Price,
//		}
//	}
//	//3。遍历priceMap删除不在范围时间，重新获取当前priceMap最大值和最小值
//	for k,v := range this.PriceMap{
//		if k >= this.newTime - this.Interval{
//			if v.low  < temp_min {
//				temp_min = v.low
//			}
//			if v.high > temp_max {
//				temp_max = v.high
//			}
//		}else {
//			delete(this.PriceMap,k)
//		}
//	}
//	// 当前的最大值、最小值、最新值
//	this.MyKline.high = temp_max
//	this.MyKline.low = temp_min
//	this.MyKline.close = trade.Price
//	this.WaveOrder()
//	this.lck.Unlock()
//}
//
//
////Wave
//func (this *StrategyWildFluctuation)WaveOrder(){
//	if util.Volatility(this.MyKline.high , this.MyKline.close) > this.OpenWave{
//		// 爆跌
//		//发开仓单
//		strategyInfo := this.GetStrategyInfo(util.BUY)
//		strategyInfo.OpenPrice = this.MyKline.close
//		strategyInfo.ClosePrice = this.MyKline.close *(1+ this.CloseWave)
//		models.UpdateStrategyInfo(strategyInfo)
//		this.SendLimitOrder(util.OPEN)
//		this.initKline()
//		return
//	}else if util.Volatility(this.MyKline.close,this.MyKline.low) > this.OpenWave{
//		// 爆涨
//		log.Println("爆涨")
//		//发开仓单
//		//var os OrderServer
//		//if x,ok := mycache.OrderServerCache.Get(this.AccountName);ok{
//		//	os = x.(OrderServer)
//		//	os.LimitOrder(this.Symbol,goex.FloatToString(this.MyKline.close,10),goex.FloatToString(this.Amount,10),goex.OPEN_SELL)
//		//}
//		log.Printf("当前：%s,最高价：%f,最低价格:%f,最新价:%f",this.StrategyName,this.MyKline.high,this.MyKline.low,this.MyKline.close)
//		this.initKline()
//		return
//	}
//}
//
//
//
////初始化kline
//func (this *StrategyWildFluctuation)initKline()  {
//	this.MyKline = &kline{
//		high: -math.MaxFloat64,
//		low: math.MaxFloat64,
//	}
//	this.PriceMap = make(map[int64] *kline)
//}
//
////获得StrategyInfo
//func (this *StrategyWildFluctuation)GetStrategyInfo(side int) (*models.StrategyInfo) {
//	if this.StrategyInfo != nil && this.StrategyInfo.StatusUse {
//		return this.StrategyInfo
//	}
//	return this.addStrategyInfo(side)
//}
//
////添加StrategyInfo
//func (this *StrategyWildFluctuation)addStrategyInfo(side int) *models.StrategyInfo {
//	strategyInfo := &models.StrategyInfo{
//		AccountName: this.AccountName,
//		StrategyName: this.StrategyName,
//		OpenAmount: this.Amount,
//		CloseAmount: this.Amount,
//		Side: side,
//		AllCycle: 1,
//		StatusUse: true,
//	}
//
//	strategyInfo, ok := models.AddStrategyInfo(strategyInfo)
//	if ok{
//		this.StrategyInfo = strategyInfo
//		return strategyInfo
//	}else {
//		return nil
//	}
//}
//
////发送限价订单
//func (this *StrategyWildFluctuation)SendLimitOrder(OpenOrClose int) bool {
//	var os OrderServer
//	if x,ok := mycache.OrderServerCache.Get(this.AccountName);ok{
//		os = x.(OrderServer)
//		switch OpenOrClose {
//		case util.OPEN:
//			switch this.StrategyInfo.Side {
//			case util.BUY:
//				order := os.LimitOrder(this.Symbol,goex.FloatToString(this.StrategyInfo.OpenPrice,10),goex.FloatToString(this.StrategyInfo.OpenAmount,10),util.OPEN_BUY)
//				if order != nil{
//					this.StrategyInfo.OpenCId = order.Cid
//				}
//			case util.SELL:
//				order := os.LimitOrder(this.Symbol,goex.FloatToString(this.StrategyInfo.OpenPrice,10),goex.FloatToString(this.StrategyInfo.OpenAmount,10),util.OPEN_SELL)
//				if order != nil{
//					this.StrategyInfo.OpenCId = order.Cid
//				}
//			}
//		case util.CLOSE:
//			switch this.StrategyInfo.Side {
//			case util.BUY:
//				order := os.LimitOrder(this.Symbol,goex.FloatToString(this.StrategyInfo.OpenPrice,10),goex.FloatToString(this.StrategyInfo.OpenAmount,10),util.CLOSE_BUY)
//				if order != nil{
//					this.StrategyInfo.CloseCId = order.Cid
//				}
//			case util.SELL:
//				order := os.LimitOrder(this.Symbol,goex.FloatToString(this.StrategyInfo.OpenPrice,10),goex.FloatToString(this.StrategyInfo.OpenAmount,10),util.CLOSE_SELL)
//				if order != nil{
//					this.StrategyInfo.CloseCId = order.Cid
//				}
//			}
//		}
//		models.UpdateStrategyInfo(this.StrategyInfo)
//	}
//	return false
//}
//
////当订单成交的时候判断，是否需要发平仓单等。。。。
//func (this *StrategyWildFluctuation)Handle(order models.Order)  {
//	//1.判断订单类型
//	if order.Cid == this.StrategyInfo.OpenCId{
//		if order.Status == util.OVER_DONE{
//			this.SendLimitOrder(util.CLOSE)
//		}
//		this.StrategyInfo.OpenExecutedQty = order.ExecutedQty
//	}else if order.Cid == this.StrategyInfo.CloseCId{
//		if order.Status == util.OVER_DONE{
//			this.StrategyInfo.NewCycle += 1
//			if this.StrategyInfo.NewCycle == this.StrategyInfo.AllCycle{
//				this.StrategyInfo.StatusUse = false
//			}else {
//				this.SendLimitOrder(util.OPEN)
//			}
//			// 设置开仓订单平仓
//			openOrder := models.FindOrder(this.StrategyInfo.OpenCId)
//			openOrder.StatusUse = false
//			models.UpdateOrder(openOrder)
//		}
//		this.StrategyInfo.CloseExecutedQty = order.ExecutedQty
//	}else {
//		StrategyLog.Error("没有找到对应等订单cid")
//	}
//	models.UpdateStrategyInfo(this.StrategyInfo)
//}